
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/08/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.technical.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.application.role.entity.Role;
import com.application.technical.entity.Technical;
import com.application.technical.struts.form.TechnicalForm;
import com.application.usertable.entity.Usertable;
import com.application.yfproject.entity.YfProject;
import com.manager.BaseDao;
import com.manager.commons.CommonUtil;
import com.manager.commons.CurrentUserUtils;
import com.manager.commons.DateUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:TechnicalDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 左磊 QQ：30575951 <br />
 * 
 * History: <br />
 * 
 * 02/08/2012
 * 
 */
public class TechnicalDAO extends HibernateGenericDao<Technical> {
	private static final Logger logger = Logger
			.getLogger(TechnicalDAO.class);

	private Technical technical;

	public Technical getTechnical() {
		return this.technical;
	}

	public void setTechnical(Technical technical) {
		this.technical = technical;
	}

	public Integer getAllCountTechnical(TechnicalForm technicalForm) {
		// 储存查询条件的buffer
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();

		buf
				.append("SELECT COUNT(0) FROM technical t left join usertable u on u.id = t.operateid  left join department b on t.orgid = b.id WHERE 1=1   ");
		// 查询字符串
		buf = getStringBuffer(technicalForm, buf, args);
		return super.getCount(buf.toString(), args.toArray());
	}

	public void deleteTechnicals(TechnicalForm technicalForm) {
		String a = technicalForm.getIds().replaceAll(",", "','");
		String hql = " delete from Technical where id in  ( '" + a + "')";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteTechnicals failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchTechnical(final TechnicalForm technicalForm,
			final PageBean pageBean) {

		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();
		buf
				.append("SELECT t.*,u.rname as uname,b.name as orgname FROM technical t left join usertable u on u.id = t.operateid  left join department b on t.orgid = b.id WHERE 1=1  ");
		buf = getStringBuffer(technicalForm, buf, args);
		buf.append(" order by t.createDate Desc ");
		return getPageInfo(pageBean, buf.toString(), args);
	}

	public List searchTechnical(final TechnicalForm technicalForm) {
		List args = new ArrayList();
		StringBuffer buf = new StringBuffer();
		buf
				.append("SELECT t.*,u.rname as uname,b.name as orgname   FROM technical t left join usertable u on u.id = t.operateid  left join department b on t.orgid = b.id WHERE 1=1  ");
		buf = getStringBuffer(technicalForm, buf, args);
		buf.append(" order by t.createDate Desc ");
		return getPageInfo(null, buf.toString(), args);
	}

	/**
	 * 查询条件的封装
	 * 
	 * @param technicalForm
	 * @return
	 */
	private StringBuffer getStringBuffer(TechnicalForm technicalForm,
			StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(technicalForm.getId())) {
			buf.append(" and t.id = '" + technicalForm.getId().trim() + "'");
		} else {
			if (StringUtils.isNotBlank(technicalForm.getTitle())) {
				buf.append(" and t.title like '%"
						+ technicalForm.getTitle().trim() + "%'");
			}
			if (StringUtils.isNotBlank(technicalForm.getType())) {
				buf.append(" and t.type = '"
						+ technicalForm.getType().trim() + "'");
			}
			if (StringUtils.isNotBlank(technicalForm.getUname())) {
				buf.append(" and u.rname like '%"
						+ technicalForm.getUname().trim() + "%'");
			}
			if (StringUtils.isNotBlank(technicalForm.getOrgname())) {
				buf.append(" and b.name like '%"
						+ technicalForm.getOrgname().trim() + "%'");
			} 
			if (StringUtils.isNotBlank(technicalForm.getBak1())) {
				buf.append(" and u.createdate >= '"
						+ technicalForm.getBak1().trim() + "-00-00'");
				buf.append(" and u.createdate <= '"
						+ technicalForm.getBak1().trim() + "-12-31'");
			}
		}

		return buf;
	}
}
